//
//  HEBaseNavController.m
//  Healthy
//
//  Created by 聂志康 on 2018/6/14.
//  Copyright © 2018年 weipainetwork. All rights reserved.
//

#import "HEBaseNavController.h"
#import "LSLoginViewController.h"
#import "LSMainViewController.h"

@interface HEBaseNavController ()<UIGestureRecognizerDelegate,UINavigationControllerDelegate>
@property (nonatomic,weak) UIViewController* currentShowVC;

@property (nonatomic,strong) NSMutableArray *snapshotArray;
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIPanGestureRecognizer *pan;
@property (nonatomic, assign) CGPoint startPoint;
@property (nonatomic, assign) CGPoint endPoint;
@end

@implementation HEBaseNavController
+ (void)initialize {
    
    // MARK: - 1.设置导航栏的背景
    // 1.获取当行条的外观代理对象！
    // 外观代理对象，决定了navBar将来的展示效果！可以认为是一个单例！
    // 只要在这里设置了外观，将来所有通过这种类型创建的对象外观都一样！
    UINavigationBar *navBar = [UINavigationBar appearance];
    // 1.1 设置背景图片
    //    [navBar setBackgroundColor:[UIColor blueColor]];
    [navBar setBackgroundImage:[UIImage imageWithColor:NAV_COLOR] forBarMetrics:UIBarMetricsDefault];
    //黑色半透明导航栏，不能设置navBar背景色，translucent需要设置成YES
    //    [navBar setBarStyle:UIBarStyleBlackTranslucent];
    
    //    [navBar setBarTintColor:NAV_COLOR];
    
    // 屏蔽阴影,消除navgationBar下面的那条线
//    navBar.shadowImage = [UIImage new];
    
    // 1.2 设置标题颜色
    [navBar setTitleTextAttributes:@{
                                     NSFontAttributeName:[UIFont fontWithName:@"SourceHanSansCN-Regular" size:17],
                                     NSForegroundColorAttributeName : UIColor.whiteColor,
                                     }];
    
    // 1.3 设置按钮的颜色 将导航栏两侧的按钮设置为白色！
    [navBar setTintColor:[UIColor whiteColor]];
}

#pragma mark - 每次跳转时都会来到这个方法！
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 如果是纯代码编写的程序，需要判断一下
    // 纯代码写在启动第一次显示时，会走一次push方法！
    if (self.viewControllers.count >= 1) {
        viewController.hidesBottomBarWhenPushed=YES;
    } else {
        viewController.hidesBottomBarWhenPushed=NO;
    }
    //判断调转的控制器与当前控制器相同的话，不再跳转
//    if (viewController.class == self.viewControllers.lastObject.class) {
//        //do nothing
//    } else {
//        [super pushViewController:viewController animated:animated];
//    }
    [super pushViewController:viewController animated:animated];
}

-(void)pop {
    [self popViewControllerAnimated:YES];
}

- (UIViewController *)childViewControllerForStatusBarStyle{
    return self.topViewController;
}

-(UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}

-(id)initWithRootViewController:(UIViewController *)rootViewController
{
    HEBaseNavController* nvc = [super initWithRootViewController:rootViewController];
//    self.interactivePopGestureRecognizer.delegate = self;
    nvc.delegate = self;
    return nvc;
}

-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    // 判断要显示的控制器是否是自己
    BOOL isShowHomePage = [viewController isKindOfClass:[LSLoginViewController class]] || [viewController isKindOfClass:[LSMainViewController class]];
    [self setNavigationBarHidden:isShowHomePage animated:YES];
}


-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if (navigationController.viewControllers.count == 1)
        self.currentShowVC = Nil;
    else
        self.currentShowVC = viewController;
}


//-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
//    if (gestureRecognizer == self.interactivePopGestureRecognizer) {
//        return (self.currentShowVC == self.topViewController); //the most important
//    }
//    return YES;
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    // 获取系统自带滑动手势的target对象
//    id target = self.interactivePopGestureRecognizer.delegate;
//    // 创建全屏滑动手势，调用系统自带滑动手势的target的action方法
//    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
//    // 设置手势代理，拦截手势触发
//    pan.delegate = self;
//    // 给导航控制器的view添加全屏滑动手势
//    [self.view addGestureRecognizer:pan];
//    // 禁止使用系统自带的滑动手势
//    self.interactivePopGestureRecognizer.enabled = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
